/**
 * @file factory_mode_testreport.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_FACTORY_MODE!= 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static void factorymode_testreport_prepare_destory(lv_obj_t * activity_obj);
static void testrepot_clean_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void testreport_item_create(lv_obj_t * par);
//static void testreport_item_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static lv_testreport_obj_ext_t * testreport_get_ext(void);
static void testrepot_retest_btn_event_cb(lv_obj_t * btn, lv_event_t e);
/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * factorymode_testreport_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_FACTORY_MODE_TESTREPORT;
        activity_ext.create = factorymode_testreport_create;
        activity_ext.prepare_destory = factorymode_testreport_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    /*Allocate the object type specific extended data*/
    lv_testreport_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_testreport_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;

    lv_factory_obj_ext_t * ext_fac = factorymode_get_ext();
    uint8_t i = 0;
    bool pass = true;
    for(i = 0; i < AUTOTEST_ITEM_NUM; i++) {
        if(ext_fac->istest[i] == true && ext_fac->test_res[i] != TEST_SUCCESS) {
            pass = false;
            break;
        }
    }
    printf("testreport pass = %d\n", pass);
    if(pass) {
        lv_obj_t * bg = lv_cont_create(obj, NULL);
        lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
        lv_cont_set_layout(bg, LV_LAYOUT_CENTER);
        lv_obj_add_style(bg, LV_CONT_PART_MAIN, &ext_fac->green_st);
        lv_watch_obj_add_element(bg);

        lv_obj_t * label_res = lv_label_create(bg, NULL);
        lv_obj_add_style(label_res, LV_LABEL_PART_MAIN, &lv_watch_font30);
        lv_label_set_text_id(label_res, WATCH_TEXT_ID_ALL_PASS);
    } else {
        lv_obj_t * label = lv_label_create(obj, NULL);
        lv_obj_set_size(label, LV_HOR_RES, LV_VER_RES / 6);
        lv_label_set_text_id(label, WATCH_TEXT_ID_TEST_REPORT);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
        lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);

        lv_obj_t * label_sign = lv_label_create(obj, label);
        lv_label_set_text_id(label_sign, WATCH_TEXT_ID_TEST_REPORT_SIGN);
        lv_label_set_long_mode(label_sign, LV_LABEL_LONG_BREAK);
        lv_obj_set_size(label_sign, LV_HOR_RES, 30);
        lv_obj_align(label_sign, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 30);

        lv_obj_t * btn_clean = lv_btn_create(obj, NULL);
        lv_obj_t * label_clean = lv_label_create(btn_clean, label);
        lv_label_set_text_id(label_clean, WATCH_TEXT_ID_TEST_REPORT_CLEAN);
        lv_obj_set_size(btn_clean, LV_HOR_RES * 3 / 5, 30);
        lv_obj_align(btn_clean, label_sign, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
        lv_obj_set_event_cb(btn_clean, testrepot_clean_btn_event_cb);

        lv_obj_t * btn_retest = lv_btn_create(obj, NULL);
        lv_obj_t * label_retest = lv_label_create(btn_retest, label);
        lv_label_set_text_id(label_retest, WATCH_TEXT_ID_RETEST);
        lv_obj_set_size(btn_retest, LV_HOR_RES * 2 / 5, 30);
        lv_obj_align(btn_retest, label_sign, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 0);
        lv_obj_set_event_cb(btn_retest, testrepot_retest_btn_event_cb);

        testreport_item_create(obj);

        ext_fac->testmode = TEST_MANUAL;
        ext_fac->lcdarea_click_num = 0;
        ext_fac->speaker_volume = 10;
        ext_fac->module = AUTOTEST_MAX;
        ext_fac->label_key_state = NULL;
        ext_fac->timer = NULL;
    }

    return obj;
}

void testreport_update(uint8_t num)
{
    lv_testreport_obj_ext_t * ext = testreport_get_ext();
    lv_factory_obj_ext_t * ext_fac = factorymode_get_ext();
    if(ext_fac->test_res[num] == TEST_FAILED) {
        lv_obj_add_style(ext->btn[num], LV_BTN_PART_MAIN, &(ext_fac->red_st));
    } else if(ext_fac->test_res[num] == TEST_SUCCESS) {
        lv_obj_add_style(ext->btn[num], LV_BTN_PART_MAIN, &(ext_fac->green_st));
    } else {
        lv_obj_add_style(ext->btn[num], LV_BTN_PART_MAIN, &(ext_fac->white_st));
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void factorymode_testreport_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    autotest_hal_stop();
}

static lv_testreport_obj_ext_t * testreport_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_FACTORY_MODE_TESTREPORT);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_testreport_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void testrepot_clean_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        uint8_t i = 0;
        lv_factory_obj_ext_t * ext_fac = factorymode_get_ext();
        factorymode_clear_test_res();
        for(i = 0; i < AUTOTEST_ITEM_NUM; i++) {
            if(ext_fac->istest[i])
                testreport_update(i);
        }
    }
}

static void testrepot_retest_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        lv_watch_png_cache_all_free();
        lv_factory_obj_ext_t * ext = factorymode_get_ext();
        ext->module = AUTOTEST_MAX;
        lv_obj_del(lv_watch_get_top_activity_obj());
        factorymode_autotest_create(NULL);
    }
}

static void testreport_item_create(lv_obj_t * par)
{
    lv_factory_obj_ext_t * ext_fac = factorymode_get_ext();
    uint8_t i = 0;
    uint8_t item_cnt = 0;
    for(i = 0; i < AUTOTEST_ITEM_NUM; i++) {
        if(ext_fac->istest[i] == true) {
            item_cnt++;
        }
    }
    lv_obj_t * page  = lv_page_create(par, NULL);
    lv_obj_set_size(page, LV_HOR_RES - 10, 130);
    lv_obj_align(page, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 5, 0);
    lv_obj_add_style(page, LV_PAGE_PART_BG, &lv_style_transp);
    lv_obj_add_style(page, LV_PAGE_PART_SCROLLABLE, &lv_style_transp);
    lv_obj_add_style(page, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_page_set_scrollbar_mode(page, LV_SCROLLBAR_MODE_OFF);

    lv_obj_t * cont = lv_cont_create(page, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 10, (item_cnt / 3 + 1) * 50 + 10);
    lv_cont_set_layout(cont, LV_LAYOUT_GRID);
    lv_obj_set_click(cont, false);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 3);
    lv_watch_obj_add_element(cont);
    lv_page_glue_obj(cont, true);

    lv_testreport_obj_ext_t * ext = testreport_get_ext();
    lv_obj_t * label[AUTOTEST_ITEM_NUM];
    for(i = 0; i < AUTOTEST_ITEM_NUM; i++) {
        if(ext_fac->istest[i]) {
            ext->btn[i] = lv_btn_create(cont, NULL);
            lv_obj_add_protect(ext->btn[i], LV_PROTECT_PRESS_LOST);
            lv_obj_set_size(ext->btn[i], (LV_HOR_RES - 10 - 3 * 3) / 3, 50);
            // lv_obj_set_event_cb(ext->btn[i], testreport_item_btn_event_cb);
            lv_watch_set_free_num(ext->btn[i], i);

            label[i] = lv_label_create(ext->btn[i], NULL);
            lv_label_set_align(label[i], LV_LABEL_ALIGN_CENTER);
            lv_label_set_long_mode(label[i], LV_LABEL_LONG_BREAK);
            lv_obj_set_width(label[i], lv_obj_get_width(ext->btn[i]));
            lv_label_set_text_id(label[i], autotest_get_item_textId(i));
            lv_obj_set_click(label[i], false);

            lv_watch_obj_add_element(ext->btn[i]);
            lv_page_glue_obj(ext->btn[i], true);
            testreport_update(i);
        }
    }
}

#if 0
void testreport_item_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        uint8_t num;
        lv_obj_t * obj;
        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_FACTORY_MODE_TESTREPORT);
        obj = lv_watch_obj_create(activity_obj);
        LV_ASSERT_MEM(obj);

        num = lv_obj_get_user_data(btn).user_num;
        autotest_test_item_create(obj, num);
    }
}
#endif
#endif /*USE_LV_WATCH_FACTORY_MODE*/
